package yxy.cherry.paycenter.servlet.order;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Set;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import com.alibaba.fastjson.JSONObject;

import yxy.apple.logger.Logger;
import yxy.apple.util.Base64Utility;
import yxy.cherry.paycenter.channelmodule.ActType;
import yxy.cherry.paycenter.channelmodule.jhh5.PayMoudle_jhh5;
import yxy.cherry.paycenter.channelmodule.sdk_huawei.PayMoudle_huawei;
import yxy.cherry.paycenter.channelmodule.sdk_huawei_quick.PayMoudle_huawei_quick;
import yxy.cherry.paycenter.channelmodule.sdk_jh.PayMoudle_jh;
import yxy.cherry.paycenter.channelmodule.sdk_oppo_quick.PayMoudle_oppo_quick;
import yxy.cherry.paycenter.channelmodule.sdk_shengye.PayMoudle_shengye;
import yxy.cherry.paycenter.channelmodule.sdk_vivo_quick.PayMoudle_vivo_quick;
import yxy.cherry.paycenter.channelmodule.sdk_xiaomi_quick.PayMoudle_xiaomi_quick;
import yxy.cherry.paycenter.channelmodule.test.PayMoudle_test;
import yxy.cherry.paycenter.channelmodule.weixin.PayMoudle_weixin;
import yxy.cherry.paycenter.channelmodule.yxy.PayMoudle_yxy;
import yxy.cherry.paycenter.database.db.orders;
import yxy.cherry.paycenter.util.HttpUtil;
import yxy.game.pm2.bean.Currency;
import yxy.game.pm2.bean.channel.SdkChannel;

//创建订单，响应CMD_CreateOrder
public class OrderServlet extends HttpServlet {

	//支付相关
	private static int NoChannel = 6200;	//渠道不存在
	private static int NoGoods = 6201;		//商品不存在
	private static int HadGetGoods = 6202;	//商品限购，不能购买
	private static int PayFail = 6203;		//购买失败
	private static int PaySucess = 6206;	//购买成功

	static final Logger logger = Logger.create(OrderServlet.class);
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		HttpUtil.printlnParseMap("get", req);
		System.out.println("sdk");
	}

	@Override
	public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		JSONObject rjson = new JSONObject();

		try {
			HttpUtil.printlnParseMap("post", req);
			JSONObject json = HttpUtil.getParseJSON(req);
			logger.info("orderservlet:" + json);

			if(!json.containsKey("channel")) {
				rjson.put("result", NoChannel);
				returnInfo(rjson, resp);
				return;
			}
			String channel = json.getString("channel");
			//@TODO 测试用
//		channel = SdkChannel.sdk_huawei_quick;

			if(!json.containsKey("goodsid")) {
				rjson.put("result", NoGoods);
				returnInfo(rjson, resp);
				return;
			}
			int dataid = json.getIntValue("goodsid");

			if(!json.containsKey("pcode")) {
				rjson.put("result", NoGoods);
				returnInfo(rjson, resp);
				return;
			}
			String pcode = json.getString("pcode");

			if(!json.containsKey("sid")) {
				rjson.put("result", NoGoods);
				returnInfo(rjson, resp);
				return;
			}
			int sid = json.getIntValue("sid");

			int acttype = json.getIntValue("acttype");
			int vipupexp = json.getIntValue("vipupexp");

			logger.info("channel {}", channel);
			orders order = null;
			switch (channel) {
				case SdkChannel.sdk_test:{ //内网测试
					PayMoudle_test pm = new PayMoudle_test();
					orders o = pm.createOrderYA(dataid, pcode, sid, acttype);
					rjson = pm.createOrderInfo(json, o);
					order = o;
					break;
				}
				case SdkChannel.sdk_yxy:{
					PayMoudle_yxy pm = new PayMoudle_yxy();
					orders o = pm.createOrderYA(dataid, pcode, sid, acttype);
					rjson = pm.createOrderInfo(json, o);
					order = o;
					break;
				}
				case SdkChannel.sdk_jh:{
					PayMoudle_jh pm = new PayMoudle_jh();
					orders o = pm.createOrderJH(dataid, pcode, sid, acttype);
					rjson = pm.createOrderInfo(json, o);
					order = o;
					break;
				}
				case SdkChannel.sdk_huawei:{
					PayMoudle_huawei pm = new PayMoudle_huawei();
					orders o = pm.createOrderHUAWEI(dataid, pcode, sid, acttype);
					rjson = pm.createOrderInfo(json, o);
					order = o;
					break;
				}
				case SdkChannel.sdk_huawei_quick:{
					PayMoudle_huawei_quick pm = new PayMoudle_huawei_quick();
					orders o = pm.createOrderHUAWEIQUICK(dataid, pcode, sid, acttype);
					rjson = pm.createOrderInfo(json, o);
					order = o;
					break;
				}
				case SdkChannel.sdk_vivo_quick:{
					PayMoudle_vivo_quick pm = new PayMoudle_vivo_quick();
					orders o = pm.createOrderVIVOQUICK(dataid, pcode, sid, acttype);
					rjson = pm.createOrderInfo(json, o);
					order = o;
					break;
				}
				case SdkChannel.sdk_oppo_quick:{
					PayMoudle_oppo_quick pm = new PayMoudle_oppo_quick();
					orders o = pm.createOrderOPPOQUICK(dataid, pcode, sid, acttype);
					rjson = pm.createOrderInfo(json, o);
					order = o;
					break;
				}
				case SdkChannel.sdk_xiaomi_quick:{
					PayMoudle_xiaomi_quick pm = new PayMoudle_xiaomi_quick();
					orders o = pm.createOrderXIAOMIQUICK(dataid, pcode, sid, acttype);
					rjson = pm.createOrderInfo(json, o);
					order = o;
					break;
				}
				case SdkChannel.sdk_weixin:{
					PayMoudle_weixin pm = new PayMoudle_weixin();
					orders o = pm.createOrderWeiXin(dataid, pcode, sid, acttype);
					rjson = pm.createOrderInfo(json, o);
					order = o;
					break;
				}
				case SdkChannel.sdk_shengye:{
					PayMoudle_shengye pm = new PayMoudle_shengye();
					orders o = pm.createOrderWeiXin(dataid, pcode, sid, acttype);
					rjson = pm.createOrderInfo(json, o);
					order = o;
					break;
				}
				case SdkChannel.sdk_jhh5:{
					PayMoudle_jhh5 pm = new PayMoudle_jhh5();
					orders o = pm.createOrderJHH5(dataid, pcode, sid, acttype);
					rjson = pm.createOrderInfo(json, o);
					order = o;
					break;
				}

				default:
					rjson.put("result", NoChannel);
					returnInfo(rjson, resp);
					break;
			}

			rjson.put("acttype", acttype);
			rjson.put("myorderid", order.getMyorderid());
			rjson.put("playercode", order.getPlayercode());
			logger.info("rjson:" + rjson.toString());
			String sendstr = Base64Utility.encodeToString(rjson.toString());
			returnInfo(sendstr, resp);

			if(channel.equals(SdkChannel.sdk_yxy)) {
				ActType at = ActType.getat(acttype);
				PayMoudle_yxy am = new PayMoudle_yxy();
				ArrayList<Currency> curs = am.getRewardInfoByType(pcode, at, dataid);
				am.createOrderInfoModule(order, curs, vipupexp, order.getGoodsprice());
			}
			if(channel.equals(SdkChannel.sdk_test)) {
				ActType at = ActType.getat(acttype);
				PayMoudle_test am = new PayMoudle_test();
				ArrayList<Currency> curs = am.getRewardInfoByType(pcode, at, dataid);
				am.createOrderInfoModule(order, curs, vipupexp, order.getGoodsprice());
			}
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("OrderServlet:%s", e.getMessage());
			logger.error("OrderServlet:%s", e);
			rjson.put("result", PayFail);
			returnInfo(rjson, resp);
			return;
		}
	}
	
	/**
	 * 返回信息
	 * 
	 * @param sendobj
	 * @param resp
	 */
	private void returnInfo(JSONObject sendobj, HttpServletResponse resp) {
		returnInfo(sendobj.toString(), resp);
	}

	/**
	 * 返回信息
	 * @param sendstr
	 * @param resp
	 */
	private void returnInfo(String sendstr, HttpServletResponse resp) {
		PrintWriter pw = null;
		resp.addHeader("Access-Control-Allow-Origin", "*");
		resp.addHeader("Content-Type", "application/json");
//		jsonRequest.setHeader("Content-Type", "application/json");
		try {
			pw = resp.getWriter();
			pw.write(sendstr);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (pw != null) {
				pw.close();	
			}
		}
	}
	
	public static NameValuePair[] getValuePair(JSONObject parseobj) {
		NameValuePair[] pair = new NameValuePair[parseobj.size()];

		Set<String> set = parseobj.keySet();
		int i = 0;
		for (String key : set) {
			pair[i] = new BasicNameValuePair(key, parseobj.getString(key));
			i++;
		}

		return pair;
	}
}
